package org.apache.sad.core.framework.jaxrpc;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.soap.SOAPElement;
import javax.xml.rpc.soap.SOAPFaultException;

import org.apache.commons.lang.StringUtils;

import org.apache.sad.core.framework.constants.ServiceConstants;

public class ServiceContext implements ServiceConstants, Serializable {
	private static final long serialVersionUID = 1L;

	private Map<String, Object> values = new HashMap<String, Object>();
 
    private SOAPElement samlAssertionElement;

	public ServiceContext() {}

    public void clear() {
    	values.clear();
    }

    public void set(String key, Object value) {
        values.put(key, value);
    }

    public String get(String key) {
        return (String) values.get(key);
    }

    public String getTrackingId() {
        return (String) this.values.get(TRACKING_ID);
    }

    public void setTrackingId(String trackingId) {
        this.values.put(TRACKING_ID, trackingId);
    }

    public String getNamespace() {
        return (String) this.values.get(NAMESPACE);
    }

    public void setNamespace(String namespace) {
        this.values.put(NAMESPACE, namespace);
    }

    public String getUserId() {
        return (String) this.values.get(USER_ID);
    }

    public void setUserId(String userId) {
        this.values.put(USER_ID, userId);
    }

    public String getSourceServerId() {
        return (String) this.values.get(SOURCE_SERVER_ID);
    }

    public void setSourceServerId(String serverId) {
        this.values.put(SOURCE_SERVER_ID, serverId);
    }

    public String getSourceSystemId() {
        return (String) this.values.get(SOURCE_SYSTEM_ID);
    }

    public void setSourceSystemId(String systemId) {
        this.values.put(SOURCE_SYSTEM_ID, systemId);
    }


    public String getServiceVersion() {
        return (String) this.values.get(SERVICE_VERSION);
    }

    public void setServiceVersion(String serviceVersion) {
        this.values.put(SERVICE_VERSION, serviceVersion);
    }

    public String getTimestamp() {
        return (String) this.values.get(TIMESTAMP);
    }

    public void setTimestamp(String timestamp) {
        this.values.put(TIMESTAMP, timestamp);
    }

    public void setWsSecurityUser(String user) {
    	this.values.put(WS_USER_ID, user);
    }

    public String getWsSecurityUser() {
    	return (String)this.values.get(WS_USER_ID);
    }

    public void setWsSecurityCredential(String password) {
    	this.values.put(WS_CREDENTIAL, password);
    }

    public String getWsSecurityCredential() {
    	return (String)this.values.get(WS_CREDENTIAL);
    }

    public void setWsSecurityCredentialType(String credentialType) {
    	this.values.put(WS_CREDENTIAL_TYPE, credentialType);
    }

    public String getWsSecurityCredentialType() {
    	return (String)this.values.get(WS_CREDENTIAL_TYPE);
    }

	public String getMessageId() {
		return (String)this.values.get(MESSAGE_ID);
	}

	public void setMessageId(String messageId) {
		this.values.put(MESSAGE_ID, messageId);
	}

	public SOAPElement getSamlAssertionElement() {
		return samlAssertionElement;
	}

	public void setSamlAssertionElement(SOAPElement samlAssertionElement) {
		this.samlAssertionElement = samlAssertionElement;
	}

    public String getCsgEnvironment() {
        return (String) this.values.get(CSG_ENVIRONMENT);
    }

    public void setCsgEnvironment(String csgEnvironment) {
        this.values.put(CSG_ENVIRONMENT, csgEnvironment);
    }

    public String getCsgConnectionMethod() {
        return (String) this.values.get(CSG_CONNECTION_METHOD);
    }

    public void setCsgConnectionMethod(String csgConnectionMethod) {
        this.values.put(CSG_CONNECTION_METHOD, csgConnectionMethod);
    }
    
    public String getAwdEnvironment() {
        return (String) this.values.get(AWD_ENVIRONMENT);
    }

    public void setAwdEnvironment(String awdEnvironment) {
        this.values.put(AWD_ENVIRONMENT, awdEnvironment);
    }
    

    public boolean useNonRealTimeData() {
        return "true".equalsIgnoreCase((String)this.values.get(USE_NON_REALTIME_DATA));
    }

    public void setUseNonRealTimeData(String useNonRealTimeData) {
        this.values.put(USE_NON_REALTIME_DATA, useNonRealTimeData);
    }    
    
    public void setTasLoggingLevel(String tasLoggingLevel) {
    	this.values.put(TAS_LOGGING_LEVEL, tasLoggingLevel);
    }
    
    public String getTasLoggingLevel() {
    	return (String)this.values.get(TAS_LOGGING_LEVEL);
    }
    
    public void setTasLogged(String tasLogged) {
    	this.values.put(TAS_LOGGED, tasLogged);
    }
    
    public boolean getTasLogged() {
    	try
    	{
    		String tasLogged = (String)this.values.get(TAS_LOGGED);
    		if (tasLogged == null || tasLogged.length() == 0)
    			return false;
    		
    		return Boolean.valueOf(tasLogged);
    	}
    	catch (Exception e) {
    	}
    	return false;
    }    
    
    public void setServiceOperation(String serviceOperation) {
    	this.values.put(SERVICE_OPERATION, serviceOperation);
    }
    
    public String getServiceOperation() {
    	return (String)this.values.get(SERVICE_OPERATION);
    }
    
    public void setServerNode(String serverNode) {
    	this.values.put(SERVER_NODE, serverNode);
    }
    
    public String getServerNode() {
    	return (String)this.values.get(SERVER_NODE);
    }
    
    public void setService(String service) {
    	this.values.put(SERVICE, service);
    }
    
    public String getService() {
    	return (String)this.values.get(SERVICE);
    }
    
    /**
	 * Gets the {@link #loggingInThreshold}.
	 *
	 * @return The {@link #loggingInThreshold}.
	 */
	public Boolean getLoggingInThreshold() {
    	try
    	{
    		String loggingInThreshold = (String)this.values.get(LOGGING_IN_THRESHOLD);
    		if (StringUtils.isEmpty(loggingInThreshold))
    			return null;
    		
    		return Boolean.valueOf(loggingInThreshold);
    	}
    	catch (Exception e) {
    	}
    	return null;
	}
	
	/**
	 * Sets the {@link #loggingInThreshold}.
	 *
	 * @param loggingInThreshold The {@link #loggingInThreshold}.
	 */
	public void setLoggingInThreshold(String loggingInThreshold) {
    	this.values.put(LOGGING_IN_THRESHOLD, loggingInThreshold);
    }
	
    public void setManagedNode(String managedNode) {
    	this.values.put(MANAGED_NODE, managedNode);
    }
    
    public String getManagedNode() {
    	return (String)this.values.get(MANAGED_NODE);
    }
    
    public void setStartDateTime(Date startDateTime) {
    	this.values.put(START_TIME, startDateTime);
    }
    
    public Date getStartDateTime() {
    	return (Date)this.values.get(START_TIME);
    }	    
    
    public void setSoapFaultException(SOAPFaultException spe) {
    	this.values.put(SOAP_FAULT_EXCEPTION, spe);
    }
    
    public SOAPFaultException getSoapFaultException() {
    	return (SOAPFaultException)this.values.get(SOAP_FAULT_EXCEPTION);
    }
    
    public String getTxnKey() { 
    	return (String)this.values.get(TXN_KEY);
    }
    
    public void setTxnKey(String txnKey) {
    	this.values.put(TXN_KEY, txnKey);
    }

    public String getMarket() { 
    	return (String)this.values.get(MARKET);
    }
    
    public void setMarket(String market) {
    	this.values.put(MARKET, market);
    }
    
    public String getBillingSystem() { 
    	return (String)this.values.get(BILLING_SYSTEM);
    }
    
    public void setBillingSystem(String billingSystem) {
    	this.values.put(BILLING_SYSTEM, billingSystem);
    }
    
    @SuppressWarnings("unchecked")
    public Map<String, String> getAspects() { 
    	return (Map<String, String>)this.values.get(ASPECTS);
    }
    
    public void setAspects(Map<String, String> aspects) {
    	this.values.put(ASPECTS, aspects);
    }
    
    public void setPercentageLogging(String percentageLogging) {
    	this.values.put(PERCENTAGE_LOGGING, percentageLogging);
    }
    
    public String getPercentageLogging() {
    	return (String)this.values.get(PERCENTAGE_LOGGING);
    }
    
    public void setRequestSize(String requestSize){
    	this.values.put(REQUEST_SIZE, requestSize);
    }
    
    public String getRequestSize(){
    	return (String)this.values.get(REQUEST_SIZE);
    }
    
    public void setResponseSize(String responseSize){
    	this.values.put(RESPONSE_SIZE, responseSize);
    }
    
    public String getResponseSize(){
    	return (String)this.values.get(RESPONSE_SIZE);
    }
    
    public void setUserGroups(List<String> userGroups) {
    	this.values.put(WS_USER_GROUPS, userGroups);
    }
    
    @SuppressWarnings("unchecked")
	public List<String> getUserGroups() {
    	return (List<String>)this.values.get(WS_USER_GROUPS);
    }
    
    public void setWsaReplyTo(String wsaReplyTo) {
    	this.values.put(WSA_REPLY_TO, wsaReplyTo);
    }
    
    public String getWsaReplyTo(){
    	return (String)this.values.get(WSA_REPLY_TO);
    }
    
    public void setWsaAction(String wsaAction) {
    	this.values.put(WSA_ACTION, wsaAction);
    }
    
    public String getWsaAction(){
    	return (String)this.values.get(WSA_ACTION);
    }
    
    public void setWsaMessageId(String wsaMessageId) {
    	this.values.put(WSA_MESSAGE_ID, wsaMessageId);
    }
    
    public String getWsaMessageId(){
    	return (String)this.values.get(WSA_MESSAGE_ID);
    }
    
    public void setWsaRelatesTo(String wsaRelatesTo) {
    	this.values.put(WSA_RELATES_TO, wsaRelatesTo);
    }
    
    public String getWsaRelatesTo(){
    	return (String)this.values.get(WSA_RELATES_TO);
    }
    
    public void setAddWsaHeader(Boolean addWsaHeader){
    	this.values.put(ADD_WSA_HEADER, addWsaHeader);
    }
    
    public Boolean getAddWsaHeader(){
    	Boolean addWsaHeader = (Boolean) this.values.get(ADD_WSA_HEADER);
    	if (addWsaHeader == null || addWsaHeader == false){
    		return false;
    	}
    	return addWsaHeader;
    } 
    
    
}